package com.pinyoumei.app.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.pinyoumei.app.activity.wallet.WalletRechargeActivity;
import com.pinyoumei.app.utils.EventBusUtils;
import com.pinyoumei.app.utils.WXShare;
import com.pinyoumei.app.utils.event.AlipayEvent;
import com.pinyoumei.app.utils.event.WechatPay;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = ".WXPayEntryActivity";
    private IWXAPI api;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_pay_success_layout);
        api = WXAPIFactory.createWXAPI(this, WXShare.APP_ID);
        api.handleIntent(getIntent(), this);

    }



    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {

    }

    @Override
    public void onResp(BaseResp resp) {
        String tip = "";
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (resp.errCode == BaseResp.ErrCode.ERR_OK) {
                EventBusUtils.post(new WechatPay("1"));
                EventBusUtils.post(new AlipayEvent("1"));
                tip = "支付成功";
                Toast.makeText(this, tip, Toast.LENGTH_SHORT).show();
                this.finish();
//                mPayStatusView.setText(tip);
            } else if (resp.errCode == BaseResp.ErrCode.ERR_COMM) {
                tip = "支付失败";
                Toast.makeText(this, tip, Toast.LENGTH_SHORT).show();
//                mPayStatusView.setText(tip);
                this.finish();
            } else if (resp.errCode == BaseResp.ErrCode.ERR_USER_CANCEL) {
                tip = "取消支付";
                Toast.makeText(this, tip, Toast.LENGTH_SHORT).show();
//                mPayStatusView.setText(tip);
                this.finish();
            } else {
                this.finish();
            }
//            final AlertDialog.Builder builder = new AlertDialog.Builder(this);
//            builder.setTitle("提示");
//            builder.setMessage("微信支付结果：" + tip);
//            final String finalTip = tip;
//            builder.setPositiveButton("返回", new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface dialog, int which) {
//                    if (finalTip.equals("支付成功")) {
//                        EventBus.getDefault().post(new MessageEvent(MessageEvent.REFRESH_MY_INTEGRAL_EVENT));
//                        EventBus.getDefault().post(new MessageEvent(MessageEvent.REFRESH_POSTER_DRAW_BUY));
//                    }
//                    builder.create().dismiss();
//                    finish();
//                }
//            });
//            builder.show();
        }
    }



    @Override
    public void onBackPressed() {
        super.onBackPressed();
//        EventBus.getDefault().post(new MessageEvent(MessageEvent.REFRESH_MY_INTEGRAL_EVENT));
//        EventBus.getDefault().post(new MessageEvent(MessageEvent.REFRESH_POSTER_DRAW_BUY));
    }


}